package com.kotiln.lgc.lib_base.base

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.MenuItem
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.kotiln.lgc.lib_base.R
import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission


/**
 *@fileName BaseActivity
 *@date2021/4/27  14:46
 *@author BaseActivity$
 *@describe TODO
 *@desc
 */
open abstract class BaseActivity : AppCompatActivity() {

     val codeWindowRequest=1000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        //屏蔽软键盘
//        window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)

//5 沉浸式状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = resources.getColor(R.color.redcolor)

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            supportActionBar?.let {
                it.title=getTitleText()
                it.setDisplayHomeAsUpEnabled(isShowBack())
                it.elevation=0f //透明值
            }
        }

        //初始化View
        initView()
        //初始化数据
        initData()

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId==android.R.id.home){
            finish()
        }
        return true
    }

    //检查窗口权限
    protected fun checkWindowPermission():Boolean{
        //当前版本大于等于6.0系统
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){
            //是否绘制在其它应用之前
            return Settings.canDrawOverlays(this)
        }
        return true
    }

    //申请窗口权限
    protected fun requestWindowPermission(packageName: String){
        //当前版本大于android6.0
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            startActivityForResult(
                Intent(
                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:$packageName")
                ), codeWindowRequest
            )
        }
    }

    //检查权限
    protected fun checkPermission(permission: String):Boolean{
        //当前版本大于android6.0
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            return checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
        }
        return true
    }

    //请求权限
    protected fun requestPermission(permission: Array<String>, granted: Action<List<String>>){
        //当前版本大于android6.0
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //权限android6.0及以上
            AndPermission.with(this)
                .runtime()
                .permission(Permission.RECORD_AUDIO)
                .onGranted(granted)
                .start()
        }
    }

    //获取布局ID
    abstract fun getLayoutId():Int

    //获取标题
    abstract fun getTitleText():String

    //是否显示返回键
    abstract fun isShowBack():Boolean

    //初始化视图
    abstract fun initView()

    //初始化数据
    abstract fun initData()

    //销毁数据
    abstract fun destoryData()

}